#!/usr/bin/env python
#encoding=utf8
#----The SimpleOperator is just used for preLex,and the other used for both preLex and lex---------
SimpleOperator=('+','-','*','/','=','<','>',)
Separation=(';','{','}','(',')',)
ArrayStart=('[',)
ArrayEnd=(']',)
Digit=('0', '1', '2', '3', '4', '5', '6', '7', '8', '9',)
Letter=('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',\
 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z','a', 'b',\
 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o','p', 'q',\
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',)
Point=('.',)
Underline=('_',)
Blank=(' ','\t',)
EOL=('\n','\r','\r\n',)
#----These are used for lex-----------------
Keyword=('if','else','while','read','write','int','real',)
Computing=('+','-','*','/',)
Comparation=('==','<','<>','>','<=','>=',)
Assign=('=',)
CommentOneline=('//',)
CommentStart=('/*',)
CommentEnd=('*/',)

#------These are used for Syntax -------------
IfKeyword=('Keyword','if',)
ElseKeyword=('Keyword','else',)
WhileKeyword=('Keyword','while',)
ReadKeyword=('Keyword','read',)
WriteKeyword=('Keyword','write',)
IntKeyword=('Keyword','int',)
RealKeyword=('Keyword','real',)
Identify=('Identify',)
AssignOp=('Assign','=',)
StatementEnd=('Separation',';',)
Integer=('Integer',)
Real=('Real',)
ArrayStartOp=('ArrayStart','[',)
ArrayEndOp=('ArrayEnd',']',)
MulOp=(('Computing','*',),('Computing','/',),)
AddOp=(('Computing','+',),('Computing','-',),)
ComparationOp=(('Comparation','=='),('Comparation','<'),('Comparation','<>'),('Comparation','>='),('Comparation','<='),)
DefineWord=(IntKeyword,RealKeyword,)

#------以下实现的是树的节点的数据结构，采用左孩子右兄弟------
class Node(object):
    def __init__(self):
        self.value=[]
    def __str__(self):
        return str(self.value)
    def add(self,val):
        self.value.append(val)
    def get(self,val):
        return self.value[val]
    def getAll(self):
        return self.value
    def length(self):
        return len(self.value)

#-------以下是代码类的实现------------------
#-------以下储存的dict的key的tuple的具体的值，所以都是key[1]----
class CodeObject(object):
    def __init__(self):
        #--name采用统一格式为key:[name,type,size=(1,),value={},linenumber]---等号表示默认
        self.__name={}
        self.__code=[]
        self.__codeNumber=0
    def hasName(self,name):
        return self.__name.has_key(name['name'])
    def getName(self,name):
        return self.__name[name['name']]
    def setNameValue(self,name,value,pos=(0)):
        #if pos==None:pos=(0,)
        nameNow=self.__name[name['name']]
        if pos<nameNow['size']:
            nameNow['value'][pos]=value
            return True
        else:
            return False
    def addNameValue(self,name,value):
        nameNow=self.__name[name['name']]
        nameNow[3].append(value)
    def getNameValue(self,name,pos=(0)):
        #if pos==None:pos=(0,)
        #print 'in symbols',pos
        nameNow=self.__name[name['name']]
        if pos<nameNow['size']:
            if nameNow['value'].has_key(pos)==False:
                #print nameNow['type']
                if nameNow['type']=='Real':
                    temp=('Real',0.0,0)
                    return temp
                else:
                    temp=('Integer',0,0)
                    return temp
            else:
                return nameNow['value'][pos]
        else:
            return False
    def getNames(self):
        return self.__name
    def addName(self,name):
        self.__name[name['name']]=name
    def setNameType(self,name,type):
        nameNow=self.__name[name['name']]
        nameNow['type']=type
    def getNameType(self,name):
        return self.__name[name['name']]['type']
    def setNameSize(self,name,size):
        #print name
        nameNow=self.__name[name['name']]
        #print name,size
        nameNow['size']=size
    def getNameSize(self,name):
        return self.__name[name['name']]['size']
    def setNameLine(self,name,line):
        self.__name[name['name']]['line']=line
    def getNameLine(self,name):
        return self.__name[name['name']]['line']
    def getCodes(self):
        return self.__code
    def getCode(self,num):
        return self.__code[num]
    def addCode(self,code):
        self.__code.append(code)
        self.__codeNumber=len(self.__code)-1
        return self.__codeNumber
    def removeCode(self,n):
        del self.__code[n]
        self.__codeNumber=len(self.__code)
    def getCodeAmount(self):
        return len(self.__code)
    def setCode(self,n,code):
        self.__code[n]=code

#------以下是栈的数据结构的实现------------------
class Stack(object):
    def __init__(self):
        self.__value=[]
    def pop(self):
        return self.__value.pop()
    def push(self,val):
        self.__value.append(val)
    def top(self):
        if self.__value.__len__()>0:
            return self.__value[-1]
        else:
            return None
    def isEmpty(self):
        return True if len(self.__value)==0 else False


